﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_EaspLibs;
using System.Web;
using System.Data;
using System.Web.Security;
using V5_EaspLibs.DBUtility;

namespace V5_FrameWork.Data {
    /// <summary>
    /// 碎片管理
    /// </summary>
    public class BaseBlockerHelper {
        private string ThemeDir = "cupertino";
        /// <summary>
        /// 碎片初始化
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public string GetBlockerInit(System.Web.UI.Page page) {
            string Action = Easp.Str.SetString(HttpContext.Current.Request["Action"]);
            string AdminID = string.Empty;
            StringBuilder tSb = new StringBuilder();
            if (IsAdmin()
                && Action.ToLower().Trim() == "goblocker") {
                tSb.AppendLine(string.Format(@"
                        <link href=""{0}Scripts/jquery/jquery-ui/{1}/jquery-ui.css"" rel=""stylesheet"" type=""text/css"" />
                        <link type=""text/css"" rel=""Stylesheet"" href=""{0}Scripts/jquery/jquery-blocker/jquery-Blocker.css"" />
                        <script src=""{0}Scripts/jquery/jquery-blocker/jquery-blocker.js"" type=""text/javascript""></script>
                        <script src=""{0}Scripts/jquery/jquery-ui/jquery-ui.js"" type=""text/javascript""></script>
                ", page.ResolveUrl("~/"), ThemeDir));

                tSb.AppendLine(@"<script type=""text/javascript"">
                        $(function() {
                            $(this).Blocker();
                        });
                </script>");
            }
            return tSb.ToString();
        }
        /// <summary>
        /// 碎片块初始化
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public string GetBlockerInit(System.Web.UI.Control page) {
            string Action = Easp.Str.SetString(HttpContext.Current.Request["Action"]);
            string AdminID = string.Empty;
            StringBuilder tSb = new StringBuilder();
            if (IsAdmin()
                && Action.ToLower().Trim() == "goblocker") {
                tSb.AppendLine(string.Format(@"
                        <link href=""{0}Scripts/jquery/jquery-ui/{1}/jquery-ui.css"" rel=""stylesheet"" type=""text/css"" />
                        <link type=""text/css"" rel=""Stylesheet"" href=""Scripts/jquery-blocker/jquery-Blocker.css"" />
                        <script src=""{0}Scripts/jquery.js"" type=""text/javascript""></script>
                        <script src=""Scripts/jquery-blocker/jquery-blocker.js"" type=""text/javascript""></script>
                        <script src=""{0}Scripts/jquery/jquery-ui/jquery-ui.js"" type=""text/javascript""></script>
                ", page.ResolveUrl("~/"), ThemeDir));

                tSb.AppendLine(@"<script type=""text/javascript"">
                        $(function() {
                            $(this).Blocker();
                        });
                </script>");
            }
            return tSb.ToString();
        }

        /// <summary>
        /// 获取碎片块
        /// </summary>
        /// <param name="blockerID"></param>
        /// <returns></returns>
        public string GetBlocker(string blockerID) {
            string Action = Easp.Str.SetString(HttpContext.Current.Request["Action"]);
            string AdminID = string.Empty;
            StringBuilder tSb = new StringBuilder();
            if (IsAdmin() && Action == "goblocker") {
                DataTable dt = GetBlockerView(blockerID);
                if (dt != null && dt.Rows.Count > 0) {
                    DataRow dr = dt.Rows[0];
                    tSb.Append(@"<div blockerid=""" + dr["BlockerID"] + @""" class=""blocker"" blockerdesc=""" + dr["BlockerDesc"] + @""">");
                    tSb.Append(dr["BlockerContent"].ToString());
                    tSb.Append("</div>");
                }
                else {
                    tSb.Append(@"<div blockerid=""" + blockerID + @""" blockername="""" blockerdesc="""" class=""blocker"">");
                    tSb.Append("</div>");
                }
            }
            else if (IsAdmin()
                && Action == "testall") {
                string txtBlockerID = Easp.Str.SetString(HttpContext.Current.Request["hfBlockerID"]);
                string txtBlockerContent = Easp.Str.SetString(HttpContext.Current.Request["txtBlockerContent"]);
                tSb.Append(txtBlockerContent);
            }
            else {
                DataTable dt = GetBlockerView(blockerID);
                if (dt != null && dt.Rows.Count > 0) {
                    string content = dt.Rows[0]["BlockerContent"].ToString();
                    //解析标签
                    content = new BaseWidgetHelper().PraseTagMode(content);
                    tSb.Append(content);
                }
            }
            return tSb.ToString();
        }

        /// <summary>
        /// 获取Blocker
        /// </summary>
        /// <param name="blockerID"></param>
        /// <returns></returns>
        private DataTable GetBlockerView(string blockerID) {
            string SQLString = string.Empty;
            if (Easp.DB.DbType == DataBaseType.OleDb || Easp.DB.DbType == DataBaseType.SqlServer) {
                SQLString = "Select Top 1 * From S_Blocker Where BlockerID='" + blockerID + "'";
            }
            else {
                SQLString = "Select * From S_Blocker Where BlockerID='" + blockerID + "' limit 1 ";
            }
            DataTable dt = Easp.DB.ExecuteQuery(SQLString).Tables[0];
            return dt;
        }
        /// <summary>
        /// 是否管理员
        /// </summary>
        /// <returns></returns>
        public bool IsAdmin() {
            if (HttpContext.Current.Request.Cookies["admins"] != null
                && HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName] != null) {
                return true;
            }
            return false;
        }
    }
}
